
package com.jf.cloud.biz.controller.multishop;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Maps;
import com.jf.cloud.api.auth.feign.AccountFeignClient;
import com.jf.cloud.api.auth.vo.AuthAccountVO;
import com.jf.cloud.api.multishop.feign.ShopUserFeignClient;
import com.jf.cloud.biz.dto.SendAndCheckSmsDTO;
import com.jf.cloud.biz.service.SmsLogService;
import com.jf.cloud.common.cache.util.RedisUtil;
import com.jf.cloud.common.constant.SendTypeEnum;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ResponseEnum;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.common.util.PrincipalUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * @author zz
 * @Date 2021/6/10 14:34
 */
@RestController("multishopSmsController")
@RequestMapping("/m/sms")
@Tag(name = "multishop-短信接口")
public class SmsController {

    @Autowired
    private SmsLogService smsLogService;
    @Autowired
    private AccountFeignClient accountFeignClient;
    @Autowired
    private ShopUserFeignClient shopUserFeignClient;
    public static final String CHECK_UPDATE_SMS_FLAG = "checkUpdatePwdSmsFlag";

    @PutMapping("/send_withdraw_code")
    @Operation(summary = "给商家手机号发送申请提现验证码" , description = "给商家手机号发送申请提现验证码，返回商家手机号")
    public ServerResponseEntity<String> applyShopSms() {
        Long shopId = AuthUserContext.get().getTenantId();
        Integer sysType = AuthUserContext.get().getSysType();
        ServerResponseEntity<AuthAccountVO> merchantInfoRes = accountFeignClient.getAccountInfoByTenantId(shopId, sysType);
        if (!merchantInfoRes.isSuccess()) {
            throw new LuckException("服务器繁忙");
        }
        smsLogService.sendSmsCode(SendTypeEnum.VALID, merchantInfoRes.getData().getPhone(), Maps.newHashMap());
        return ServerResponseEntity.success(merchantInfoRes.getData().getPhone());
    }

    @PutMapping("/send_auth_shop_user_code")
    @Operation(summary = "发送认证店铺员工/子账号验证码" , description = "发送认证店铺员工/子账号验证码")
    public ServerResponseEntity<Void> sendAuthShopUserSms(@RequestBody SendAndCheckSmsDTO sendAndCheckSmsDTO) {
        String phone = sendAndCheckSmsDTO.getMobile();
        if (StrUtil.isBlank(phone) || !PrincipalUtil.isMobile(phone)) {
            throw new LuckException("手机号格式不正确");
        }
        smsLogService.sendSmsCode(SendTypeEnum.VALID, phone, Maps.newHashMap());
        return ServerResponseEntity.success();
    }

    /**
     * 发送忘记密码验证码接口
     */
    @PostMapping("/ua/update_pwd")
    @Operation(summary = "发送忘记密码验证码接口" , description = "发送忘记密码验证码接口")
    public ServerResponseEntity<Void> sendUpdatePwdCode(@RequestBody SendAndCheckSmsDTO sendSmsParam) {
        if (Objects.equals(shopUserFeignClient.countAccoutByMobile(sendSmsParam.getMobile()).getData(), 0)) {
            throw new LuckException("该手机号码未注册");
        }
        // 每个手机号每分钟只能发十个注册的验证码，免得接口被利用
        smsLogService.sendSmsCode(SendTypeEnum.UPDATE_PASSWORD, sendSmsParam.getMobile(), Maps.newHashMap());
        return ServerResponseEntity.success();
    }

    @PutMapping("/ua/check_update_pwd")
    @Operation(summary = "校验用户忘记密码时的验证码" , description = "校验验证码返回校验成功的标识")
    public ServerResponseEntity<String> checkUpdatePwdSms(@Valid @RequestBody SendAndCheckSmsDTO sendAndCheckSmsDto) {
        // 每个ip每分钟只能发十个注册的验证码，免得接口被利用
        if (!smsLogService.checkValidCode(sendAndCheckSmsDto.getMobile(), sendAndCheckSmsDto.getValidCode(), SendTypeEnum.UPDATE_PASSWORD)) {
            // 验证码有误或已过期
            return ServerResponseEntity.fail(ResponseEnum.VERIFICATION_CODE_ERROR);
        }
        String checkUpdatePwdSmsFlag = IdUtil.simpleUUID();
        RedisUtil.set(CHECK_UPDATE_SMS_FLAG + checkUpdatePwdSmsFlag, sendAndCheckSmsDto.getMobile(), 600);
        return ServerResponseEntity.success(checkUpdatePwdSmsFlag);
    }

}
